
package com.htd.htdapmservice.xxljob;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.htd.htdapmapi.alarm.request.AlarmInto;
import com.htd.htdapmapi.alarm.result.AlarmResult;
import com.htd.htdapmcommon.constants.ErrorCodeConstants;
import com.htd.htdapmcommon.enums.AlarmSendStatus;
import com.htd.htdapmcommon.enums.AlarmsLevelEnum;
import com.htd.htdapmcommon.enums.AlarmsStatusEnum;
import com.htd.htdapmdmo.DataCompare;
import com.htd.htdapmservice.dao.DataCompareDao;
import com.htd.htdapmservice.service.intf.OneAlertService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;


@JobHandler(value="dataCompareAlarmJobHandler")
@Component
public class DataCompareAlarmJobHandler extends IJobHandler {
    private static final Logger logger = LoggerFactory.getLogger(DataCompareAlarmJobHandler.class);

    @Autowired
    private DataCompareDao dataCompareDao;

    @Autowired
    private OneAlertService  oneAlertService;

	@Override
	public ReturnT<String> execute(String param) throws Exception {
		logger.info("dataCompareAlarmJobHandler Entrance, param : {}", new Object[]{ JSONObject.toJSONString(param)});
        List<DataCompare> dataCompareList = new ArrayList<DataCompare>();
        try{
        	
            dataCompareList = dataCompareDao.findDataCompareListByStatus(AlarmSendStatus.SEND_NO.getCode());
            
            if (dataCompareList!=null &&dataCompareList.size()>0) {
                for (DataCompare dataCompare : dataCompareList) {
                	AlarmInto alarmInto=new AlarmInto();
                	BeanCopier copier = BeanCopier.create(DataCompare.class, AlarmInto.class, false);
            		copier.copy(dataCompare,alarmInto, null);
                	alarmInto.setEventType(AlarmsStatusEnum.ALARM_TRIGGER.getCode());
                	alarmInto.setPriority(AlarmsLevelEnum.LEVEL_TWO.getCode());
                	alarmInto.setProductType(dataCompare.getProductTypeCode());
                    AlarmResult alarmResult = oneAlertService.createAlarm(alarmInto);
                    //创建告警成功时，更新dataCompare表中status的状态为“SEND_YES”
					if (ErrorCodeConstants.SUCCESS_CODE.equals(alarmResult.getResponseCode())) {
						Map<String, Object> paramMap = new HashMap<>();
						paramMap.put("id", dataCompare.getId());
						paramMap.put("status", AlarmSendStatus.SEND_YES.getCode());
						dataCompareDao.updateAlarmStatus(paramMap);
					}
                }
            }
            
        } catch (Exception e){
            logger.error("dataCompareAlarmJobHandler error:", e);
            return FAIL;
        }
        
        return SUCCESS;
        
	}

	

}


